UpdateSchoolAction.index   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 42
rs 9.208
c 0
b 0
f 0
cc 2
1
import {
2
  Body,
3
  Controller,
4
  Inject,
5
  BadRequestException,
6
  UseGuards,
7
  Put,
8
  Param
9
} from '@nestjs/common';
10
import { AuthGuard } from '@nestjs/passport';
11
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
12
import { ICommandBus } from 'src/Application/ICommandBus';
13
import { UpdateSchoolCommand } from 'src/Application/School/Command/UpdateSchoolCommand';
14
import { UserRole } from 'src/Domain/User/User.entity';
15
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
16
import { Roles } from 'src/Infrastructure/User/Decorator/Roles';
17
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard';
18
import { SchoolDTO } from '../DTO/SchoolDTO';
19
20
@Controller('schools')
21
@ApiTags('School')
22
@ApiBearerAuth()
23
@UseGuards(AuthGuard('bearer'), RolesGuard)
24
export class UpdateSchoolAction {
25
  constructor(
26
    @Inject('ICommandBus')
27
    private readonly commandBus: ICommandBus
28
  ) {}
29
30
  @Put(':id')
31
  @Roles(UserRole.PHOTOGRAPHER)
32
  @ApiOperation({ summary: 'Update school' })
33
  public async index(@Param() idDto: IdDTO, @Body() dto: SchoolDTO) {
34
    try {
35
      const {
36
        reference,
37
        name,
38
        address,
39
        zipCode,
40
        city,
41
        phoneNumber,
42
        email,
43
        numberOfClasses,
44
        numberOfStudents,
45
        observation,
46
        status,
47
        type
48
      } = dto;
49
50
      const id = await this.commandBus.execute(
51
        new UpdateSchoolCommand(
52
          idDto.id,
53
          reference,
54
          name,
55
          address,
56
          zipCode,
57
          city,
58
          status,
59
          type,
60
          email,
61
          phoneNumber,
62
          numberOfStudents,
63
          numberOfClasses,
64
          observation
65
        )
66
      );
67
68
      return { id };
69
    } catch (e) {
70
      throw new BadRequestException(e.message);
71
    }
72
  }
73
}
74